//
//  MapDetailViewController.m
//  GoldOperationClient
//
//  Created by yaxue wang on 16/9/18.
//  Copyright © 2016年 展鸿科技. All rights reserved.
//

#import "MapDetailViewController.h"
#import <BaiduMapAPI_Map/BMKMapComponent.h>
#import <BaiduMapAPI_Search/BMKSearchComponent.h>
#import <BaiduMapAPI_Utils/BMKUtilsComponent.h>
#import <BaiduMapAPI_Map/BMKMapComponent.h>
#import <BaiduMapAPI_Location/BMKLocationComponent.h>
#define MINIMUM_ZOOM_ARC 0.014 //approximately 1 miles (1 degree of arc ~= 69 miles)
#define ANNOTATION_REGION_PAD_FACTOR 1.5
#define MAX_DEGREES_ARC 360
@interface MapDetailViewController ()<BMKMapViewDelegate, BMKPoiSearchDelegate,BMKGeoCodeSearchDelegate>
{
    BMKGeoCodeSearch * _geocodesearch;
    BMKPointAnnotation* pointAnnotation;
    BMKPointAnnotation* animatedAnnotation;
}
@property (nonatomic, strong) BMKMapView* mapView;

@end

@implementation MapDetailViewController
-(void)viewWillAppear:(BOOL)animated {
    [_mapView viewWillAppear];
    _mapView.delegate = self; // 此处记得不用的时候需要置nil，否则影响内存的释放
    [self zoomMapViewToFitAnnotations:self.mapView animated:animated];

}

-(void)viewWillDisappear:(BOOL)animated {
    [_mapView viewWillDisappear];
    _mapView.delegate = nil; // 不用时，置nil
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navBar.hidden=YES;
    _mapView =[[BMKMapView alloc]initWithFrame:CGRectMake(0, 0, WindowWidth, WindowHeight)];
    _mapView.delegate = self; // 不用时，置nil
    [self.view addSubview:_mapView];
    
        //添加标注
        if (pointAnnotation == nil) {
            pointAnnotation = [[BMKPointAnnotation alloc]init];
            CLLocationCoordinate2D coor;
            coor.latitude = [[self.dataDic objectForKey:@"latitude1"]doubleValue];
            coor.longitude = [[self.dataDic objectForKey:@"longitude1"]doubleValue];
            pointAnnotation.coordinate = coor;
//            pointAnnotation.title = @"我的位置";
            [_mapView addAnnotation:pointAnnotation];
        }

    
    if ([[self.dataDic objectForKey:@"latitude2"] doubleValue]==0||[[self.dataDic objectForKey:@"longitude2"]doubleValue ]==0) {
        
    }else
    {
       // 添加动画Annotation
    if (animatedAnnotation == nil) {
        animatedAnnotation = [[BMKPointAnnotation alloc]init];
        CLLocationCoordinate2D coor;
        coor.latitude = [[self.dataDic objectForKey:@"latitude2"]doubleValue];
        coor.longitude = [[self.dataDic objectForKey:@"longitude2"]doubleValue];
        animatedAnnotation.coordinate = coor;
        [_mapView addAnnotation:animatedAnnotation];
        
    }
    }

    UIImage *img =[UIImage imageNamed:@"Ico_home_ close"];
    UIButton *backBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame=CGRectMake(WindowWidth-25-img.size.width, 10, img.size.width*2, img.size.height*2);
    [backBtn setImage:img forState:UIControlStateNormal];
    [backBtn bk_addEventHandler:^(id sender) {
         [self dismissViewControllerAnimated:YES completion:nil];
    } forControlEvents:UIControlEventTouchUpInside];
    [_mapView addSubview:backBtn];

}

//- (void)mapViewDidFinishLoading:(BMKMapView *)mapView
//{
//    
//    _mapView.centerCoordinate = CLLocationCoordinate2DMake([[self.dataDic objectForKey:@"latitude1"]doubleValue], [[self.dataDic objectForKey:@"longitude1"]doubleValue]);
//    
//    
//}

// 根据anntation生成对应的View
- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{
    //普通annotation
    if (annotation == pointAnnotation) {
        NSString *AnnotationViewID = @"renameMark";
        BMKPinAnnotationView *annotationView = (BMKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
        if (annotationView == nil) {
            annotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
            annotationView.annotation=annotation;
            
            UIImage *img =[UIImage imageNamed:@"Ico_Loaction"];
            annotationView.frame =CGRectMake(0, 0, img.size.width, img.size.height);
            annotationView.image =img;
        }
        return annotationView;
    }else
    {
        
        NSString *AnnotationViewID = @"AnimatedAnnotation";
        BMKPinAnnotationView *annotationView = (BMKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:AnnotationViewID];
        if (annotationView == nil) {
            annotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:AnnotationViewID];
            annotationView.annotation=annotation;
            UIImage *img =[UIImage imageNamed:@"Ico_people"];
            annotationView.frame =CGRectMake(0, 0, img.size.width, img.size.height);
            annotationView.image =img;
        }
        return annotationView;
        
    }
    
    
}

- (void)zoomMapViewToFitAnnotations:(BMKMapView *)mapView animated:(BOOL)animated
{
    NSArray *annotations = mapView.annotations;
    int count = [mapView.annotations count];
    if ( count == 0) { return; } //bail if no annotations
    
    //convert NSArray of id <MKAnnotation> into an MKCoordinateRegion that can be used to set the map size
    //can't use NSArray with MKMapPoint because MKMapPoint is not an id
    BMKMapPoint points[count]; //C array of MKMapPoint struct
    for( int i=0; i<count; i++ ) //load points C array by converting coordinates to points
    {
        CLLocationCoordinate2D coordinate = [(id <BMKAnnotation>)[annotations objectAtIndex:i] coordinate];
        points[i] = BMKMapPointForCoordinate(coordinate);
    }
    //create MKMapRect from array of MKMapPoint
    BMKMapRect mapRect = [[BMKPolygon polygonWithPoints:points count:count] boundingMapRect];
    //convert MKCoordinateRegion from MKMapRect
    BMKCoordinateRegion region = BMKCoordinateRegionForMapRect(mapRect);
    
    //add padding so pins aren't scrunched on the edges
    region.span.latitudeDelta  *= ANNOTATION_REGION_PAD_FACTOR;
    region.span.longitudeDelta *= ANNOTATION_REGION_PAD_FACTOR;
    //but padding can't be bigger than the world
    if( region.span.latitudeDelta > MAX_DEGREES_ARC ) { region.span.latitudeDelta  = MAX_DEGREES_ARC; }
    if( region.span.longitudeDelta > MAX_DEGREES_ARC ){ region.span.longitudeDelta = MAX_DEGREES_ARC; }
    
    //and don't zoom in stupid-close on small samples
    if( region.span.latitudeDelta  < MINIMUM_ZOOM_ARC ) { region.span.latitudeDelta  = MINIMUM_ZOOM_ARC; }
    if( region.span.longitudeDelta < MINIMUM_ZOOM_ARC ) { region.span.longitudeDelta = MINIMUM_ZOOM_ARC; }
    //and if there is a sample of 1 we want the max zoom-in instead of max zoom-out
    if( count == 1 )
    {
        region.span.latitudeDelta = MINIMUM_ZOOM_ARC;
        region.span.longitudeDelta = MINIMUM_ZOOM_ARC;
    }
    [mapView setRegion:region animated:animated];
}
@end
